﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is the same as in photoshop (hope so at least. ;-)
    /// </summary>
    public class Screening : BaseImageFilter, ITwoSourceFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Screening"/> class.
        /// </summary>
        public Screening()
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// Processes the specified _bitmap source.
        /// </summary>
        /// <param name="_bitmapSource">The _bitmap source.</param>
        /// <param name="_bitmapDest">The _bitmap dest.</param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmapSource, UnsafeBitmap _bitmapDest)
        {
            UnsafeBitmap bitmapSource = _bitmapSource;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;

            int nHeight = bitmapSource.Height;
            int nWidth = bitmapSource.Width;

            int nR = 0;
            int nG = 0;
            int nB = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmapSource.GetPixel(x, y);

                    nR = color.R * color.R / 255;
                    nG = color.G * color.G / 255;
                    nB = color.B * color.B / 255;
                    color = Color.FromArgb(nR, nG, nB);
                    bitmapSource.SetPixel(x, y, color);
                }
            }
            bitmapSource.Dispose();
            return _bitmapDest;
        }

        #endregion

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Screening();
        }
    }

}
